﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HF.Framework.SqlDataContract;
using HF.Framework.DataClientDBAgent.WCFDatabaseService;

namespace HF.Framework.DataClientDBAgent
{
    public  class ClientDBAgent
    {
        private IDataBaseService CreateDatabaseService()
        {
            return new DataBaseServiceClient();
        }
        public static ClientDBAgent GetClientDBAgent()
        {
            return new ClientDBAgent();
        }


        public int ExecuteNonQuery(SqlDataItem sqlItem)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.ExecuteNonQuery(sqlItem);

        }
        public bool ExecuteNonQueryArray(SqlDataItemArray arySqlDataItem)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.ExecuteNonQueryArray(arySqlDataItem);
        }
        public DataSet ExecuteQuery(SqlDataItem sqlItem)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.ExecuteQuery(sqlItem);
        }
        public DataTable ExecuteDataTable(SqlDataItem sqlItem)
        {
            return ExecuteQuery(sqlItem).Tables[0];
        }
        public string ExecuteScalar(SqlDataItem sqlDataItem)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.ExecuteScalar(sqlDataItem);
        }
        public bool RecordExists(SqlDataItem sqlItem)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.RecordExists(sqlItem);
        }
        public DbConnectionType GetDbConnectionType()
        {
            return DbConnectionType.WCF;
        }

        #region 访问当前数据库的表和字段
        public string GetCurrentDatabaseName()
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.GetCurrentDatabaseName();
        }
        public DataSet GetDBQuery(string databaseName)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.GetDBQuery(databaseName);
        }
        public DataTable GetDBDataTable(string databaseName)
        {
           
            return GetDBQuery(databaseName).Tables[0];
        }
        public DataSet GetDTColumnsQuery(string tableName, string databaseName)
        {
            IDataBaseService agent = CreateDatabaseService();
            return agent.GetDTColumnsQuery(tableName, databaseName);
        }
        public DataTable GetDTColumnsDataTable(string tableName, string databaseName)
        {
           
            return GetDTColumnsQuery(tableName,databaseName).Tables[0];
        }
        public string GetDBDateTime()
        {
       
            return GetDBDateTime();
        }
        #endregion

        #region 获得一个空结构
        public DataTable GetBlankTable(string tableName)
        {
            return GetBlankTable(tableName, null);

        }
        public DataTable GetBlankTable(string tableName, string databaseName)
        {
            //List<string> tableNames = new List<string>();
            //tableNames.Add(tableName);
            //return GetBlankDataSet(tableNames,databaseName).Tables[0];
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.DatabaseName = databaseName;
            sqlItem.CommandText = "select * from [" + tableName + "] where 1=2";
            DataTable dt = ExecuteDataTable(sqlItem);
            dt.TableName = tableName;
            return dt;
        }
        public DataSet GetBlankDataSet(List<string> tableNames)
        {
            return GetBlankDataSet(tableNames, null);
        }
        public DataSet GetBlankDataSet(List<string> tableNames, string databaseName)
        {
            //IDataBaseService agent = CreateDatabaseService();
            //return agent.GetBlankDataSet0(tableNames, databaseName);
            DataSet ds = new DataSet();
            foreach (string tbName in tableNames)
            {
                DataTable dt = GetBlankTable(tbName, databaseName);
                ds.Tables.Add(dt.Copy());
            }
            return ds;
        }
        #endregion

        #region 更新数据集
        public void UpdateDataSetWithTranse(DataSet dataSet)
        {
            UpdateDataSetWithTranse(dataSet, null);
        }
        public void UpdateDataTableWithTranse(DataTable dataTable)
        {
            UpdateDataTableWithTranse(dataTable,null);
        }
        /// <summary>
        /// 更新数据集，启动数据库事务
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="databaseName">数据库名</param>
        public void UpdateDataSetWithTranse(DataSet dataSet, string databaseName)
        {
            IDataBaseService agent = CreateDatabaseService();
            agent.UpdateDataSetWithTranse0(dataSet, databaseName);
        }
        /// <summary>
        /// 更新表,启动数据库事务
        /// </summary>
        /// <param name="dataTable">要更新的表</param>
        /// <param name="databaseName">数据库名</param>
        public void UpdateDataTableWithTranse(DataTable dataTable, string databaseName)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(dataTable);
            UpdateDataSetWithTranse(ds, databaseName);
        }
        #endregion

    }
}
